// import Cookies from 'js-cookie';

const TokenKey = 'Admin-Token';
const UserInfoKey = 'User-Info';

export function getToken() {
  return localStorage.getItem(TokenKey);
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token);
}

export function removeToken() {
  return localStorage.removeItem(TokenKey);
}

/**
 * 获取用户信息
 * @returns {Object|null} 用户信息对象或null
 */
export function getUserInfo() {
  const userInfoStr = localStorage.getItem(UserInfoKey);
  return userInfoStr ? JSON.parse(userInfoStr) : null;
}

/**
 * 保存用户信息
 * @param {Object} userInfo - 用户信息对象
 */
export function setUserInfo(userInfo) {
  localStorage.setItem(UserInfoKey, JSON.stringify(userInfo));
}

/**
 * 移除用户信息
 */
export function removeUserInfo() {
  localStorage.removeItem(UserInfoKey);
}

/**
 * 检查用户是否有指定角色
 * @param {string|Array} role - 角色或角色数组
 * @returns {boolean} 是否拥有角色
 */
export function hasRole(role) {
  const userInfo = getUserInfo();
  if (!userInfo || !userInfo.roles) {
    return false;
  }
  
  if (Array.isArray(role)) {
    return role.some(r => userInfo.roles.includes(r));
  }
  
  return userInfo.roles.includes(role);
}

/**
 * 检查用户是否有指定权限
 * @param {string|Array} permission - 权限或权限数组
 * @returns {boolean} 是否拥有权限
 */
export function hasPermission(permission) {
  const userInfo = getUserInfo();
  if (!userInfo || !userInfo.permissions) {
    return false;
  }
  
  if (Array.isArray(permission)) {
    return permission.some(p => userInfo.permissions.includes(p));
  }
  
  return userInfo.permissions.includes(permission);
} 